package com.sky.service.impl;

import com.sky.dto.DataOverViewQueryDTO;
import com.sky.dto.GoodsSalesDTO;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description ReportServiceImpl
 * @Author zhuzi
 * @Date 2023/10/08
 */
@RequiredArgsConstructor
@Service
public class ReportServiceImpl implements ReportService {

    private final ReportMapper reportMapper;

    /**
     * 统计营业额
     *
     * @param data DataOverViewQueryDTO
     * @return TurnoverReportVO
     */
    @Override
    public TurnoverReportVO getTurnoverReport(DataOverViewQueryDTO data) {
        List<LocalDate> dateList = getDateList(data);
        List<BigDecimal> turnoverList = dateList.stream().map(i -> {
            BigDecimal turnover = reportMapper.getTurnoverByDate(i);
            return turnover == null ? BigDecimal.ZERO : turnover;
        }).collect(Collectors.toList());
        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    /**
     * 用户统计
     *
     * @param data DataOverViewQueryDTO
     * @return UserReportVO
     */
    @Override
    public UserReportVO getUserReport(DataOverViewQueryDTO data) {
        List<LocalDate> dateList = getDateList(data);
        List<Integer> newUserCountList = dateList.stream().map(reportMapper::getNewUserCountByDate).collect(Collectors.toList());
        Integer count = 0;
        List<Integer> totalUserCountList = new ArrayList<>(newUserCountList.size());
        for (Integer i : newUserCountList) {
            count += i;
            totalUserCountList.add(count);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(totalUserCountList, ","))
                .newUserList(StringUtils.join(newUserCountList, ","))
                .build();
    }

    /**
     * 订单统计
     *
     * @param data DataOverViewQueryDTO
     * @return OrderReportVO
     */
    @Override
    public OrderReportVO getOrderReport(DataOverViewQueryDTO data) {
        List<LocalDate> dateList = getDateList(data);
        List<Integer> orderCountList = new ArrayList<>(dateList.size());
        List<Integer> validOrderCountList = new ArrayList<>(dateList.size());
        dateList.forEach(i -> {
            orderCountList.add(reportMapper.getOrderCountByDate(i));
            validOrderCountList.add(reportMapper.getValidOrderCountByDate(i));
        });
        int totalOrderCount = orderCountList.stream().mapToInt(Integer::intValue).sum();
        int validOrderCount = validOrderCountList.stream().mapToInt(Integer::intValue).sum();
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(totalOrderCount == 0 ? 0.0 : validOrderCount * 1.0 / totalOrderCount)
                .build();
    }

    /**
     * 获取销量前10的商品（包括菜品和套餐）
     *
     * @param data DataOverViewQueryDTO
     * @return SalesTop10ReportVO
     */
    @Override
    public SalesTop10ReportVO getTop10(DataOverViewQueryDTO data) {
        List<GoodsSalesDTO> top10 = reportMapper.getTop10(data);
        List<String> nameList = new ArrayList<>(top10.size());
        List<Integer> numberList = new ArrayList<>(top10.size());
        top10.forEach(i -> {
            nameList.add(i.getName());
            numberList.add(i.getNumber());
        });
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
    }

    private List<LocalDate> getDateList(DataOverViewQueryDTO data) {
        LocalDate begin = LocalDate.from(data.getBegin());
        LocalDate end = LocalDate.from(data.getEnd());
        List<LocalDate> dateList = new ArrayList<>(Period.between(begin, end).getDays() + 1);
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }

}
